/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.projectSetup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 *
 * @author Bartek
 */
public class ProjectConfigurationWindow extends javax.swing.JPanel {

    /**
     * Creates new form ProjectConfigurationWindow
     */
    public ProjectConfigurationWindow(JFrame window) {
        initComponents();
        this.window = window;
        
        try {
            FileInputStream in = new FileInputStream("appConfig");
            
            Properties projectConfiguration = new Properties();
            projectConfiguration.load(in);
            
            sourceCodePathTextField.setText(projectConfiguration.getProperty("SourceCodePath"));
            testClassesPathTextField.setText(projectConfiguration.getProperty("TestClassesPath"));
            projectFilesPathTextField.setText(projectConfiguration.getProperty("ProjectFilesPath"));
            
            in.close();
            
        } catch (FileNotFoundException ex) {
            
        }
        catch (IOException e) {
            Logger.getLogger(ProjectConfigurationWindow.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        sourceCodePathTextField = new javax.swing.JTextField();
        chooseSourceCodePathButton = new javax.swing.JButton();
        testClassesPathTextField = new javax.swing.JTextField();
        chooseTestClassesPath = new javax.swing.JButton();
        projectFilesPathTextField = new javax.swing.JTextField();
        chooseProjectFilesPathButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        sourceCodePathTextField.setFocusable(false);

        chooseSourceCodePathButton.setText("Choose source code path");
        chooseSourceCodePathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseSourceCodePathButtonActionPerformed(evt);
            }
        });

        testClassesPathTextField.setFocusable(false);

        chooseTestClassesPath.setText("Choose test classes path");
        chooseTestClassesPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseTestClassesPathActionPerformed(evt);
            }
        });

        projectFilesPathTextField.setFocusable(false);

        chooseProjectFilesPathButton.setText("Choose project files path");
        chooseProjectFilesPathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseProjectFilesPathButtonActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(projectFilesPathTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                    .addComponent(testClassesPathTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sourceCodePathTextField, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(saveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(chooseSourceCodePathButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(chooseTestClassesPath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(chooseProjectFilesPathButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(0, 18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sourceCodePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chooseSourceCodePathButton))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testClassesPathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chooseTestClassesPath))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectFilesPathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chooseProjectFilesPathButton))
                .addGap(18, 18, 18)
                .addComponent(saveButton)
                .addContainerGap(25, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void chooseSourceCodePathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseSourceCodePathButtonActionPerformed
        showFileChooser(sourceCodePathTextField);
    }//GEN-LAST:event_chooseSourceCodePathButtonActionPerformed

    private void chooseTestClassesPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseTestClassesPathActionPerformed
        showFileChooser(testClassesPathTextField);
    }//GEN-LAST:event_chooseTestClassesPathActionPerformed

    private void chooseProjectFilesPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseProjectFilesPathButtonActionPerformed
        showFileChooser(projectFilesPathTextField);
    }//GEN-LAST:event_chooseProjectFilesPathButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        FileOutputStream fos = null;
        try {
            Properties projectConfiguration = new Properties();
            projectConfiguration.setProperty("SourceCodePath", sourceCodePathTextField.getText());
            projectConfiguration.setProperty("TestClassesPath", testClassesPathTextField.getText());
            projectConfiguration.setProperty("ProjectFilesPath", projectFilesPathTextField.getText());
            
             fos = new FileOutputStream("appConfig", false);
            
            projectConfiguration.store(fos, "Project Configuration");
            window.setVisible(false);
            
        } catch (Exception ex) {
            Logger.getLogger(ProjectConfigurationWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(ProjectConfigurationWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void showFileChooser(JTextField textFieldToUpdate) {
        if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            textFieldToUpdate.setText(fileChooser.getSelectedFile().getAbsolutePath());
        }
    }
    
    private JFrame window;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseProjectFilesPathButton;
    private javax.swing.JButton chooseSourceCodePathButton;
    private javax.swing.JButton chooseTestClassesPath;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JTextField projectFilesPathTextField;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField sourceCodePathTextField;
    private javax.swing.JTextField testClassesPathTextField;
    // End of variables declaration//GEN-END:variables
}
